#define _CRT_SECURE_NO_WARNINGS
#include "stack.h"

void STInit(STNode* st)
{
	assert(st);
	st->arr = NULL;
	st->top = 0;
	st->capacity = 0;
}
void STDestroy(STNode* st)
{
	assert(st);
	free(st->arr);
	st->top = 0;
	st->capacity = 0;
	st->arr = NULL;
}

void STPop(STNode* st)
{
	assert(st);
	//assert(st->top > 0);
	assert(!STEmpty(st));
	st->top--;
}

void STPush(STNode* st, StackDataType x)
{
	assert(st);
	if (st->top == st->capacity)
	{
		int size = st->capacity == 0 ? 4 : st->capacity * 2;
		StackDataType* tmp = (StackDataType*)realloc(st->arr, sizeof(StackDataType)*size);
		if (tmp == NULL)
		{
			perror("relloc fail");
			return;
		}
		st->arr = tmp;
		st->capacity = size;
	}
	st->arr[st->top] = x;
	st->top++;
}

StackDataType STTop(STNode* st)
{
	assert(st);
	assert(!STEmpty(st));
	StackDataType ret = st->arr[st->top-1];
	STPop(st);
	return ret;
}

bool STEmpty(STNode* st)
{
	assert(st);
	return st->top == 0;
}

int STSize(STNode* st)
{
	assert(st);
	return st->top;
}